package com.bpms.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bpms.common.VO;
import com.bpms.esb.IService;
import com.service.devimonitor.Atmpdeviceserviceimpl;
import com.service.devimonitor.Atmpdevicestatusserviceimpl;
import com.zk.atm.forms.AtmpDeviceStatusForm;
import com.zk.atm.service.impl.AtmpDeviceServiceImpl;
import com.zk.atm.utils.ConstantsAtmp;
import com.zk.atm.utils.JsonUtil;
import com.zk.atm.utils.StringUtil;

/**
 * @author zengqun
 * @remark 获取客户端：所有设备的状态,与flex端通信展示
 * @date 2012-10-8
 * @version 1.0
 */
public class DeviceMonitorServlet extends BaseServlet implements IService {

	private Atmpdevicestatusserviceimpl atmpDeviceStatusServcie;
	
	private Atmpdeviceserviceimpl atmpDeviceServiceImpl;
	
	public Atmpdevicestatusserviceimpl getIAtmpDeviceStatusService(){
		if(this.atmpDeviceStatusServcie==null){
			this.atmpDeviceStatusServcie=new Atmpdevicestatusserviceimpl();
		}
		return this.atmpDeviceStatusServcie;
	}
	
	public Atmpdeviceserviceimpl getAtmpDeviceServiceImpl() {
		if(this.atmpDeviceServiceImpl==null){
			this.atmpDeviceServiceImpl=new Atmpdeviceserviceimpl();
		}
		return this.atmpDeviceServiceImpl;
	}



	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String action=StringUtil.getPropertyValue(request.getParameter("action"));
			if(action.equals(ConstantsAtmp.ATMP_DEVICE_STATUS_ACTION_METHOD)){
			  this.status_list(request,response);
			}else if(action.equals(ConstantsAtmp.ATMP_DEVICE_ACTION_METHOD)){
			  this.device_list(request,response);
			}else if(action.equals(ConstantsAtmp.ATMP_DEVICE_ACTION_SAVE_METHOD)){
			  this.device_save(request,response);
			}else if(action.equals(ConstantsAtmp.ATMP_DEVICE_ACTION_DEL_METHOD)){
			  this.device_del(request,response);
			}
			else{
				response.getWriter().write("ERROR");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @author zengqun
	 * @remark 查询atmp_device_status_current表的状态数据
	 * @date 2012-10-9
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void status_list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		AtmpDeviceStatusForm statusForm=new AtmpDeviceStatusForm();
		try {
			String device_name=StringUtil.getPropertyValue(request.getParameter("device_name")).trim();
			String deviceip=StringUtil.getPropertyValue(request.getParameter("deviceip")).trim();
			String servicestatus=StringUtil.getPropertyValue(request.getParameter("servicestatus")).trim();
			String networkcode=StringUtil.getPropertyValue(request.getParameter("networkcode")).trim();
			statusForm.getCurrentVo().setDevice_name(device_name);
			statusForm.getCurrentVo().setDeviceip(deviceip);
			statusForm.getCurrentVo().setServicestatus(servicestatus);
			statusForm.getCurrentVo().setNetworkcode(networkcode);
			List list=this.getIAtmpDeviceStatusService().getDeviceStatusList(statusForm);
			System.out.println("list.size()="+list.size()+"*************************list="+list);
			request.setAttribute("devmonitorList", list);
			request.getRequestDispatcher("devicemonitor/devMonitorXML.jsp").forward(request, response);
		    return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author zengqun
	 * @remark 查询atmp_device设备表的数据
	 * @date 2012-10-9
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void device_list(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		AtmpDeviceStatusForm statusForm=new AtmpDeviceStatusForm();
		try {
			String device_name=StringUtil.getPropertyValue(request.getParameter("device_name")).trim();
			String deviceip=StringUtil.getPropertyValue(request.getParameter("deviceip")).trim();
			String networkcode=StringUtil.getPropertyValue(request.getParameter("networkcode")).trim();
			statusForm.getCurrentVo().setDevice_name(device_name);
			statusForm.getCurrentVo().setDeviceip(deviceip);
			statusForm.getCurrentVo().setNetworkcode(networkcode);
			List list=this.getIAtmpDeviceStatusService().getDeviceList(statusForm);
			System.out.println("list.size()="+list.size()+"*************************list="+list);
			request.setAttribute("devmonitorList", list);
			request.getRequestDispatcher("devicemonitor/deviceXML.jsp").forward(request, response);
		    return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author zengqun
	 * @remark 查询atmp_device设备表的数据
	 * @date 2012-10-9
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void device_save(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		AtmpDeviceStatusForm form=new AtmpDeviceStatusForm();
		try {
			
			String device_id=StringUtil.getPropertyValue(request.getParameter("device_id")).trim();
			String device_name=StringUtil.getPropertyValue(request.getParameter("device_name")).trim();
			String device_code=StringUtil.getPropertyValue(request.getParameter("device_code")).trim();
			String device_ip=StringUtil.getPropertyValue(request.getParameter("device_ip")).trim();
			
			String belong_org_id=StringUtil.getPropertyValue(request.getParameter("belong_org_id")).trim();
			String belong_org_name=StringUtil.getPropertyValue(request.getParameter("belong_org_name")).trim();
			String provider_name=StringUtil.getPropertyValue(request.getParameter("provider_name")).trim();
			
			String device_type_id=StringUtil.getPropertyValue(request.getParameter("device_type_id")).trim();
			String device_type_code=StringUtil.getPropertyValue(request.getParameter("device_type_code")).trim();
			String device_type_name=StringUtil.getPropertyValue(request.getParameter("device_type_name")).trim();
			
			String update_user_id=StringUtil.getPropertyValue(request.getParameter("user_id")).trim();
			String update_user_name=StringUtil.getPropertyValue(request.getParameter("user_name")).trim();
			
			form.getDevice_vo().setDevice_id(device_id);
			form.getDevice_vo().setDevice_name(device_name);
			form.getDevice_vo().setDevice_code(device_code);
			form.getDevice_vo().setDevice_ip(device_ip);
			
			form.getDevice_vo().setBelong_org_id(belong_org_id);
			form.getDevice_vo().setBelong_org_name(belong_org_name);
			form.getDevice_vo().setProvider_name(provider_name);
			
			form.getDevice_vo().setDevice_type_id(device_type_id);
			form.getDevice_vo().setDevice_type_code(device_type_code);
			form.getDevice_vo().setDevice_type_name(device_type_name);
			
			Date d=new Date();
			if(device_id.equals("")){
				//初次创建时,创建人也是修改人;当修改时,此时的创建人不变
				String create_user_id=update_user_id;
				String create_user_name=update_user_name;
				form.getDevice_vo().setCreate_user_id(create_user_id.equals("")?null:new Long(create_user_id));
				form.getDevice_vo().setCreate_user_name(create_user_name);
				form.getDevice_vo().setCreate_date(d);
			}
			form.getDevice_vo().setUpdate_user_id(update_user_id.equals("")?null:new Long(update_user_id));
			form.getDevice_vo().setUpdate_user_name(update_user_name);
			form.getDevice_vo().setUpdate_date(d);
			
			boolean is_success=this.getAtmpDeviceServiceImpl().executeSaveOrDelBySql(form);
			String message="操作失败!";
			if(is_success==true){
				message="操作成功!";
			}
			VO vo=new VO();
			vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, message);
			String json=JsonUtil.getJsonObjectByBpmVo(vo);
			response.getWriter().write(json);
		    return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author zengqun
	 * @remark 查询atmp_device设备表的数据
	 * @date 2012-10-9
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void device_del(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException  {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		AtmpDeviceStatusForm statusForm=new AtmpDeviceStatusForm();
		try {
			String device_ip=StringUtil.getPropertyValue(request.getParameter("device_ip")).trim();
			if(device_ip.equals("")){
				VO vo=new VO();
				vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "参数值不能为空,不能执行删除设备的操作!");
				String json_str=JsonUtil.getJsonObjectByBpmVo(vo);
				response.getWriter().write(json_str);
				return;
			}
			statusForm.getDevice_vo().setDevice_ip(device_ip);
			//删除之前,先判断是否有级联引用
			boolean is_refer_status_current=this.getAtmpDeviceServiceImpl().checkDeviceReferStatusCurrent(statusForm);
			VO vo=new VO();
			if(is_refer_status_current==true){
				vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "当前IP为"+device_ip+"的设备,在设备状态表中有记录,不能执行删除操作!");
			}
			
			boolean is_success=this.getAtmpDeviceServiceImpl().deleteDeviceByDeviceIp(statusForm);
			if(is_success==true){
				vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作成功!");
			}
			String json=JsonUtil.getJsonObjectByBpmVo(vo);
			response.getWriter().write(json);
			//this.device_list(request, response);
		    return;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return;
	}

	@Override
	public VO exec(VO arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
